const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    indicatorDots: false,
    vertical: false,
    autoplay: true,
    interval: 2000,
    duration: 500,
    isNoSwiper: false,
    bannerList: [],
    categories: [],
    notice: '',

    productList: [],
    user: '',
    usedPoints: 0, //累计消耗积分
    userScore: 0, //用户积分数
    diamondNum: 0, //钻石数
    userBalance: 0, //账户余额
  },

  scroll() {},
  toPage(e) {
    var flag = e.currentTarget.dataset.flag,
      id = e.currentTarget.dataset.id;
    if (flag == 'cart') {
      wx.navigateTo({
        url: '/orderPackage/pages/shoppingCart/shoppingCart',
      })
    } else if (flag == 'cate') {
      console.log(id);
      wx.setStorageSync('cateId', id)
      wx.switchTab({
        url: '/pages/classifyList/classifyList',
      })
    } else if (flag == 'detail') {
      wx.navigateTo({
        url: '/mainPackage/pages/productDetail/productDetail?id=' + id,
      })
    } else if (flag == 'search') {
      wx.navigateTo({
        url: '/mainPackage/pages/searchResult/searchResult',
      })
    } else if (flag == 'notice') {
      wx.navigateTo({
        url: '/userPackage/pages/notice/notice',
      })
    } else if (flag == 'diamond') {
      wx.navigateTo({
        url: '/mainPackage/pages/diamondMall/diamondMall',
      })
    } else if (flag == 'banner') {
      // 点击轮播图
      var banner = e.currentTarget.dataset.banner;
      console.log(banner);
      if (banner.flag == 0) {
        // 商品
        wx.navigateTo({
          url: '/mainPackage/pages/productDetail/productDetail?id=' + banner.bandingId,
        })
      } else if (banner.flag == 1 || banner.flag == 2) {
        // 科普
        this.getBaikeDetail(banner.flag, banner.bandingId)
      } else if (banner.flag == 3) {
        wx.navigateTo({
          url: banner.pictureUrl,
        })
      }
    } else if (flag == 'vip') {
      wx.navigateTo({
        url: '/mainPackage/pages/vipCenter/vipCenter',
      })
    } else if (flag == 'info') {
      wx.navigateTo({
        url: '/userPackage/pages/userInfo/userInfo',
      })
    } else if (flag == 'assets') {
      wx.navigateTo({
        url: '/userPackage/pages/asset/asset',
      })
    }

  },
  // 搜索 
  toSearch() {},

  toMySelf() {},
  infoHref() {},

  getBaikeDetail(flag, bandingId) {
    var url = app.globalData.url + "scSystem/getBandingById";
    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
        bandingId,
        flag
      }, res => {
        console.log(res);
        if (res.flag) {
          var res = res.data
          wx.hideLoading()

          console.log(res);
          wx.navigateTo({
            url: '/contentPackage/pages/baikeDetail/baikeDetail?obj=' + JSON.stringify(res),
          })
        }
      },
      err => {
        console.log(err);
      })


  },


  getMainData() {
    var url = app.globalData.url + "miniMain/listMain",
      that = this;
    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
        userId: app.globalData.userId,
        companyId: app.globalData.companyId,
      }, res => {
        console.log(res);
        if (res.flag) {
          var res = res.data
          console.log(res);

          console.log(res.bannerList);

          if (res.isUpdate != null) {
            this.uploadUser()
          }


          let result = [];
          res.bannerList.forEach(item => {
            if (result.findIndex(x => x.text === (item.bannerStyle)) == -1) {
              result.push({
                text: item.bannerStyle,
                bannerList: []
              })
            }
          })
          console.log(result);

          result.forEach(item => {
            res.bannerList.forEach(ele => {
              if (ele.bannerStyle == item.text) {
                item.bannerList.push(ele)
              }
            })
          })
          console.log(result);




          wx.hideLoading()
          wx.stopPullDownRefresh()
          that.setData({
            isNoSwiper: res.bannerList.length == 0,
            bannerList: result,
            categories: res.categories,
            notice: res.notice,
            productList: res.productList,
            // user: res.user,
          })
          console.log(res);
        }
      },
      err => {
        console.log(err);
      })
  },

  uploadUser() {
    var url = app.globalData.url + "scUser/newGetUser";
    wx.showLoading({
      title: '',
    })

    app.wxRequest(url, {
      userId: app.globalData.userId
    }, res => {
      if (res.flag) {
        console.log(res.data);
        var user = res.data;

        app.globalData.user = user
        app.globalData.openId = user.openId
        app.globalData.userId = user.userId
        app.globalData.userType = user.userType
        app.globalData.usedPoints = user.usedPoints
        app.globalData.userScore = user.userScore
        app.globalData.diamondNum = user.diamondNum
        app.globalData.userBalance = user.userBalance
        wx.setStorageSync('user', JSON.stringify(res.data))
      }
    })


  },


  getVip(level) {
    console.log(level);
    var url = app.globalData.url + "scVip/getVipLevelList";
    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
        companyId: app.globalData.companyId
      }, res => {
        console.log(res);
        if (res.flag) {
          wx.setStorageSync('nextLevelScore', JSON.stringify(res.data))
          //最高等级
          // if(level==res.data[res.data.length-1].vipLevelNumber){
          //   wx.setStorageSync('nextLevelScore', res.data[res.data.length-1])
          // }else{
          //   level +=1;
          //   res.data.forEach(item => {
          //     if (item.vipLevelNumber == level) {
          //       wx.setStorageSync('nextLevelScore', item);
          //       break;
          //     }
          //   })
          // }

          wx.hideLoading()
        }
      },
      err => {
        console.log(err);
      })

  },




  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    console.log(app);
    var isLogin = wx.getStorageSync("isXiuZhengLogin"),
      that = this;
    if (!isLogin) {
      app.getInfo().then(function (res) {
        console.log(res);
        if (res.data.user.userName == '') {
          wx.reLaunch({
            url: '/userPackage/pages/userInfo/userInfo',
          })
        }
        app.globalData.user = res.data.user
        app.globalData.openId = res.data.user.openId
        app.globalData.userId = res.data.user.userId
        app.globalData.userType = res.data.user.userType
        app.globalData.usedPoints = res.data.user.usedPoints
        app.globalData.userScore = res.data.user.userScore
        app.globalData.diamondNum = res.data.user.diamondNum
        app.globalData.userBalance = res.data.user.userBalance
        wx.setStorageSync('isXiuZhengLogin', true)
        wx.setStorageSync('user', JSON.stringify(res.data.user))



        that.getMainData()

        that.setData({
          userScore: app.globalData.userScore,
          userBalance: app.globalData.userBalance,
          diamondNum: app.globalData.diamondNum,
          usedPoints: app.globalData.usedPoints,
          user: app.globalData.user
        })
        if (!wx.getStorageSync('nextLevelScore')) {
          that.getVip(app.globalData.user.vipLevel)
        }
      })
    } else {
      if (!app.globalData.user.userName) {
        wx.reLaunch({
          url: '/userPackage/pages/userInfo/userInfo',
        })
      }
      this.getMainData()
    }
    console.log(app.globalData);
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var isLogin = wx.getStorageSync("isXiuZhengLogin");
    // 非首次登录执行
    if (isLogin) {
      if (!app.globalData.user.userName) {
        wx.reLaunch({
          url: '/userPackage/pages/userInfo/userInfo',
        })
      }
      this.setData({
        userScore: app.globalData.userScore,
        userBalance: app.globalData.userBalance,
        diamondNum: app.globalData.diamondNum,
        usedPoints: app.globalData.usedPoints,
        user: app.globalData.user
      })
      console.log(app.globalData);
      console.log(this.data);

      if (!wx.getStorageSync('nextLevelScore')) {
        this.getVip(app.globalData.user.vipLevel + 1)
      }
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getMainData()
    this.setData({
      userScore: app.globalData.userScore,
      userBalance: app.globalData.userBalance,
      diamondNum: app.globalData.diamondNum,
      usedPoints: app.globalData.usedPoints,
      user: app.globalData.user
    })
    console.log(app.globalData);
    console.log(this.data);

    if (!wx.getStorageSync('nextLevelScore')) {
      this.getVip(app.globalData.user.vipLevel + 1)
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})